package fr.esgi.pages;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.esgi.classes.Promotion;
import fr.esgi.donnees.Database;
import fr.esgi.utils.Utils;


/**
 * Servlet implementation class ValidationPromotion
 */
public class ValidationPromotion extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ValidationPromotion() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String promotion=request.getParameter("promotion");
		String groupe=request.getParameter("groupe");
		Utils u= new Utils();
		if(u.verificationPromotion(promotion)&&!promotion.isEmpty()&&promotion!=null){
			Promotion p=new Promotion(promotion.trim(),Integer.parseInt(groupe));
			Database c= new Database();
			try {
				if(c.verifPromotion(p)==0){
					c.insertPromotion(p);
				}
				else {
					request.setAttribute("erreur", "la promotion existe deja");
					getServletContext().getRequestDispatcher("/jsp/AjoutPromotion.jsp").forward(request, response);
				}
			
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			getServletContext().getRequestDispatcher("/Accueil").forward(request, response);
		}
		else {
			request.setAttribute("erreur", "erreur dans le champ promotion");
			getServletContext().getRequestDispatcher("/jsp/AjoutPromotion.jsp").forward(request, response);
		}
	
	}

}
